package com.cpi.basilos.reason.movement.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.cpi.basilos.reason.movement.dto.ReasonMovementDTO;
import com.cpi.basilos.reason.movement.exception.ReasonMovementException;
import com.cs.common.exception.CSException;
import com.cs.common.pool.DBDAO;

public class ReasonMovementDAO extends DBDAO {
	private static org.apache.log4j.Logger logger = Logger.getLogger(ReasonMovementDAO.class);
	
	private static final String SQL_GET_RESAON_MOVEMENT = "SELECT ID_MOVIM, DESCRIPCION FROM TIPO_MOVIMIENTO where CODIGO = ?";//$NON-NLS-1$
	public ReasonMovementDTO getReasonMovement(String reasonMovementCode)throws ReasonMovementException{
		ReasonMovementDTO reasonMovementDTO = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			preparedStatement = prepareSQL(SQL_GET_RESAON_MOVEMENT, dsCIMa);
			preparedStatement.setString(1, reasonMovementCode);
			resultSet = executeQuery(preparedStatement);
			if(resultSet.next()){
				reasonMovementDTO = new ReasonMovementDTO();
				reasonMovementDTO.setCode(reasonMovementCode);
				reasonMovementDTO.setId(resultSet.getInt(1));
				reasonMovementDTO.setDescription(resultSet.getString(2));
			}
			connection = preparedStatement.getConnection();
		}catch(Exception exception){
			ReasonMovementException reasonMovementException = null;
			exception.printStackTrace(System.out);
			reasonMovementException = new ReasonMovementException(exception, CSException.LAYER_DAO, CSException.ACTION_LISTS);
			logger.error(exception);
			throw reasonMovementException;
		}finally{
			try{
				closeConnection(connection);
			}catch(Exception exception){}
		}
		return reasonMovementDTO;
	}
}